﻿namespace AUSRIS.Data.Web.Entities
{
	/// <summary>
	/// <para>
	/// Represents a data source in SSRS
	/// </para>
	/// </summary>
	public class DataSource : DeploymentItem
	{

		#region Private Fields

		private string userName;
		private string password;
		private string connectionString;
		private string rsConnectionString;
		private string credentialRetrieval;
		private bool windowsCredentials;
		private bool publish;
		private bool overwrite;

		#endregion

		#region Constructor(s)

		/// <summary>
		/// <para>
		/// DataSource constructor that sets the Name, ParentFolder, UserName, Password,
		/// CredentialRetrieval, WindowsCredentials, ConnectionString, Publish and Overwrite properties when the DataSource object is created.
		/// </para>
		/// </summary>
		/// <param name="name">Name of DataSource</param>
		/// <param name="parentFolder">Parent folder of DataSource</param>
		/// <param name="userName">UserName that DataSource uses</param>
		/// <param name="password">Password for the UserName that DataSource uses</param>
		/// <param name="credentialRetrieval">Not yet determined.  Need to update this properties documentation</param>
		/// <param name="windowsCredentials"><see cref="WindowsCredentials"/><value><c>true</c> if windows credentials; otherwise, <c>false</c>.</value></param>
		/// <param name="connectionString">Connection string for DataSource</param>
		/// <param name="publish">Determines whether or not to publish DataSource</param>
		/// <param name="overwrite">Determines whether or not to overwrite pre existing DataSource during deployment</param>
		public DataSource(string name, Folder parentFolder, string userName, string password, string credentialRetrieval, bool windowsCredentials, string connectionString, bool publish, bool overwrite)
			: base(name, parentFolder)
		{
			this.userName = userName;
			this.password = password;
			this.rsConnectionString = connectionString;
			this.publish = publish;
			this.overwrite = overwrite;
			this.credentialRetrieval = credentialRetrieval;
			this.windowsCredentials = windowsCredentials;
		}

		#endregion

		#region Public Properties

		/// <summary>
		/// <para>
		/// Gets the user name.
		/// </para>
		/// </summary>
		/// <value>The user name.</value>
		public string UserName
		{
			get
			{
				return userName;
			}
		}

		/// <summary>
		/// <para>
		/// Gets the password for the given <see cref="UserName"/>
		/// </para>
		/// </summary>
		public string Password
		{
			get
			{
				return password;
			}
		}

		/// <summary>
		/// <para>
		/// CredentialRetrieval
		/// </para>
		/// </summary>
		public string CredentialRetrieval
		{
			get
			{
				return credentialRetrieval;
			}
		}

		/// <summary>
		/// <para>
		/// ConnectionString
		/// </para>
		/// </summary>
		public string ConnectionString
		{
			get
			{
				if(rsConnectionString.EndsWith(";"))
				{
					connectionString = rsConnectionString.Substring(0, rsConnectionString.Length - 1);
				}
				else
				{
					connectionString = rsConnectionString;
				}
				if(userName != null)
				{
					connectionString = string.Format("{0};uid={1};pwd={2}", connectionString, userName, password);

				}
				else
				{
					if(connectionString.ToLower().IndexOf("trusted_connection") < 0)
					{
						connectionString = string.Format("{0};Trusted_Connection=yes", connectionString);
					}
				}
				return connectionString;
			}
		}

		/// <summary>
		/// <para>
		/// Gets the connection string in RS desired format.
		/// </para>
		/// </summary>
		/// <value>The connection string in RS desired format.</value>
		public string RSConnectionString
		{
			get
			{
				return rsConnectionString;
			}
		}

		/// <summary>
		/// <para>
		/// Gets a value indicating whether this <see cref="DataSource"/> should be published.
		/// </para>
		/// </summary>
		/// <value><c>true</c> if published; otherwise, <c>false</c>.</value>
		public bool Publish
		{
			get
			{
				return publish;
			}
		}

		/// <summary>
		/// <para>
		/// Gets a value indicating whether this <see cref="DataSource"/> should be overwritten.
		/// </para>
		/// </summary>
		/// <value><c>true</c> if overwrite; otherwise, <c>false</c>.</value>
		public bool Overwrite
		{
			get
			{
				return overwrite;
			}
		}

		/// <summary>
		/// <para>
		/// Gets a value indicating whether to use windows credentials.
		/// </para>
		/// </summary>
		/// <value><c>true</c> if windows credentials; otherwise, <c>false</c>.</value>
		public bool WindowsCredentials
		{
			get
			{
				return windowsCredentials;
			}
		}

		#endregion
	}
}